import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';

typedef ValueCallback = void Function(String value);

class CustomTextField extends StatefulWidget {
  final ValueCallback? onChanged;
  final String hintText;
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final TextStyle? textStyle;
  final TextStyle? hintStyle;
  final TextAlign textAlign;
  final Color? fillColor;
  final EdgeInsetsGeometry? contentPadding;
  final List<TextInputFormatter>? inputFormatters;
  final TextInputType? keyboardType;
  final TextInputAction? textInputAction;
  final bool obscureText;
  final BorderRadius? borderRadius;
  final Widget? prefixIcon;
  final Widget? suffixIcon;
  final bool buildSearchPrefixIcon;
  final bool buildClearSuffixIcon;
  final EdgeInsetsGeometry prefixIconPadding;
  final EdgeInsetsGeometry suffixIconPadding;
  final InputBorder? enabledBorder;
  final InputBorder? focusedBorder;
  final int? maxLines;
  final int? minLines;

  CustomTextField(
    this.hintText, {
    this.controller,
    this.focusNode,
    this.onChanged,
    this.textStyle,
    this.hintStyle,
    this.textAlign = TextAlign.left,
    this.fillColor = Colours.color_white,
    this.contentPadding,
    this.inputFormatters,
    this.keyboardType,
    this.textInputAction,
    this.obscureText = false,
    this.borderRadius,
    this.prefixIcon,
    this.suffixIcon,
    this.buildSearchPrefixIcon = false,
    this.buildClearSuffixIcon = false,
    this.prefixIconPadding = EdgeInsets.zero,
    this.suffixIconPadding = EdgeInsets.zero,
    this.enabledBorder,
    this.focusedBorder,
    this.maxLines,
    this.minLines,
  });

  @override
  State<StatefulWidget> createState() {
    return _CustomTextFieldState();
  }
}

class _CustomTextFieldState extends State<CustomTextField> {
  late TextEditingController _controller;
  late FocusNode _focus = FocusNode();

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController();
    _focus = widget.focusNode ?? FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _controller,
      focusNode: _focus,
      cursorColor: Colours.color_theme,
      keyboardType: widget.keyboardType,
      textInputAction: widget.textInputAction,
      style: widget.textStyle,
      inputFormatters: widget.inputFormatters,
      obscureText: widget.obscureText,
      textAlign: widget.textAlign,
      maxLines: widget.maxLines,
      minLines: widget.minLines,
      decoration: InputDecoration(
        hintText: widget.hintText,
        hintStyle: widget.hintStyle,
        //如果为true，则输入使用fillColor指定的颜色填充
        filled: true,
        //相当于输入框的背景颜色
        fillColor: widget.fillColor,
        //内间距
        contentPadding: widget.contentPadding,
        //改变输入框是否为密集型，默认为false，修改为true时，图标及间距会变小
        isDense: true,
        isCollapsed: true,
        //去掉间距
        prefixIconConstraints: BoxConstraints(),
        prefixIcon: buildPrefixIcon(),
        //去掉间距
        suffixIconConstraints: BoxConstraints(),
        suffixIcon: buildSuffixIcon(),
        border: OutlineInputBorder(
          gapPadding: 0,
          borderRadius: widget.borderRadius ?? BorderRadius.circular(0),
          borderSide: BorderSide(
            width: 0,
            style: BorderStyle.none,
          ),
        ),
        enabledBorder: widget.enabledBorder,
        focusedBorder: widget.focusedBorder,
      ),
      onChanged: (value) {
        if (widget.suffixIcon != null) {
          if (mounted) {
            setState(() {});
          }
        }
        if (widget.onChanged != null) {
          widget.onChanged!(value);
        }
      },
    );
  }

  buildPrefixIcon() {
    if (widget.buildSearchPrefixIcon) {
      return Padding(
        padding: widget.prefixIconPadding,
        child: Image.asset("images/ic_search_gray.png"),
      );
    } else {
      return widget.prefixIcon != null
          ? Padding(
              padding: widget.prefixIconPadding,
              child: widget.prefixIcon,
            )
          : null;
    }
  }

  buildSuffixIcon() {
    if (widget.buildClearSuffixIcon) {
      return _controller.text.isNotEmpty
          ? GestureDetector(
              onTap: () {
                _controller.clear();
                _focus.unfocus();
                _focus.canRequestFocus = false;
                Future.delayed(Duration(milliseconds: 100), () {
                  _focus.canRequestFocus = true;
                });
                if (widget.onChanged != null) {
                  widget.onChanged!("");
                }
                if (mounted) {
                  setState(() {});
                }
              },
              child: Padding(
                padding: widget.suffixIconPadding,
                child: Image.asset("images/ic_clear_text.png"),
              ),
            )
          : null;
    } else {
      return widget.suffixIcon != null
          ? Padding(
              padding: widget.suffixIconPadding,
              child: widget.suffixIcon,
            )
          : null;
    }
  }
}
